package dolf.window;

import java.io.IOException;
import java.util.Properties;

import javax.swing.UIManager;

import dolf.game.BloodManager;

public class ConfigurationHandler extends Properties{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static ConfigurationHandler instance = new ConfigurationHandler();
	
	private ConfigurationHandler() {
		loadConfig();
	}
	
	public static ConfigurationHandler getInstance() {
		return instance;
	}
	
	public void loadConfig() {
		try {
			java.io.InputStream stream = new java.io.FileInputStream(System.getProperty("user.home")+java.io.File.separator+".dolf"+java.io.File.separator+"config.properties");
			
			this.load(stream);

			if (this == null) {
				System.err.println("ERROR: File config.properties ");
			}
			stream.close();
		
		} catch (IOException e) {
			System.err.println("ERROR: File config.properties " + e.getMessage());
		}
	}
	
	public void saveConfig() {
		/*save current configuration to file*/
		try {
			java.io.File targetDir =  new java.io.File(System.getProperty("user.home")+java.io.File.separator+".dolf");
			if (!targetDir.exists()) targetDir.mkdir();
			String target =  System.getProperty("user.home")+java.io.File.separator+".dolf"+java.io.File.separator+"config.properties";
			java.io.FileOutputStream stream = new java.io.FileOutputStream(target, false);
			
			if (this == null) {
				System.err.println("ERROR: File config.properties ");
			}
			else {
				this.store(stream, "dolf configuration file");
				stream.close();
			}
		
		} catch (IOException e) {
			System.err.println("ERROR: " + e.getMessage());
		}
		
		initConfig();

	}
	public void initConfig() {
		SystemMessagesHandler.getInstance().initMessages();
		
		//fenster neu zeichnen
		initWindow(AboutWindow.getInstance());
		initWindow(BuyWindow.getInstance());
		initWindow(MainWindow.getInstance());
		initWindow(NewGameWindow.getInstance());
		initWindow(PreferencesWindow.getInstance());
		
		Canvas.getInstance().setAntialiasing( ((getProperty("antialiasing").compareTo("true") == 0) ? true : false));
		Canvas.getInstance().setDithering( ((getProperty("dithering").compareTo("true") == 0) ? true : false));
		Canvas.getInstance().setInterpolation( Integer.parseInt(getProperty("interpolation")));
		
		//BloodManager einstellen
		
		if (getProperty("blood").equals("true")) {
			double val = Integer.parseInt(getProperty("bloodlevel"))/100.0;
			if (val > 1.0) val = 1.0;
			BloodManager.getInstance().changeBloodyGame(val);
		}
		else {
			BloodManager.getInstance().changeBloodyGame(0);
		}
		if (getProperty("keep_blood").equals("true")) {
			BloodManager.getInstance().changeBloodyStage(true);
		} else BloodManager.getInstance().changeBloodyStage(false);
		
		
	}
	public String getProperty(String prop) {
		String val = super.getProperty(prop);
		if (val == null){
			//default values
			
			
			if (prop.equals("save_path")) val = System.getProperty("user.home")+java.io.File.separator+".dolf";
			else if (prop.equals("log_enabled")) val = "false";
			else if (prop.equals("log_dir")) val = System.getProperty("user.home")+java.io.File.separator+".dolf";
			else if (prop.equals("show_playername")) val = "true";
			else if (prop.equals("money")) val = "500";
			else if (prop.equals("weapons")) val = "true";
			else if (prop.equals("health")) val = "100";
			else if (prop.equals("antialiasing")) val = "false";
			else if (prop.equals("interpolation")) val = "0";
			else if (prop.equals("dithering")) val = "false";
			else if (prop.equals("blood")) val = "true";
			else if (prop.equals("bloodlevel")) val = "50";
			else if (prop.equals("keep_blood")) val = "true";
			else if (prop.equals("sell_percentage")) val = "80";
			
			else if (prop.equals("lookandfeel")) val = UIManager.getCrossPlatformLookAndFeelClassName();
			else if (prop.equals("theme")) val = "Oceanarsch";
			else if (prop.equals("lang")) val = java.util.Locale.getDefault().getLanguage(); 
			
		}
		return val;
		
	}
	
	public String getProperty(String prop, String def) {
		System.out.println("deprecation warning: default values of "+prop+"("+def+") should not be set manually, ConfigurationHandler should do this for you!\nPrinting Stacktrace to find specific code");
		Thread.dumpStack();
		return super.getProperty(prop, def);
	}
	
	private void initWindow(Window w){
		if (w != null && w.isVisible()) {
			w.setVisible(false);
			w.initLookAndFeel();
			w.draw();
			w.pack();
			w.setVisible(true);
		}
	}
}
